<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Go操作Windows | lilei</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/images/icons/logo.png">
    <link rel="apple-touch-icon" href="/images/icons/logo.png">
    <link rel="manifest" href="/manifest.json">
    <script type="text/javascript" src="/assets/js/load.js"></script>
    <meta name="description" content="个人网站">
    <meta name="twitter:title" content="Go操作Windows">
    <meta name="twitter:description" content="">
    <meta name="twitter:card" content="summary">
    <meta name="twitter:url" content="http://www.lileiblog.xyz/Form/Go%E6%93%8D%E4%BD%9CWindows.html">
    <meta property="og:type" content="article">
    <meta property="og:title" content="Go操作Windows">
    <meta property="og:description" content="">
    <meta property="og:url" content="http://www.lileiblog.xyz/Form/Go%E6%93%8D%E4%BD%9CWindows.html">
    <meta property="og:site_name" content="lilei">
    <meta itemprop="name" content="Go操作Windows">
    <meta itemprop="description" content="">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/images/icons/logo.png">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="keywords" content="lilei,vue,vuepress,vuepress-theme,theme,主题,vuepress主题,blog
        ,vuepress-blog,java,python,shell,sql,golang,script,shell script,nginx,windows,去广告,androd,google drive
        ,one drive,idea,eclipse,git,小程序">
    <meta name="referrer" content="never">
    
    <link rel="preload" href="/assets/css/0.styles.8932179b.css" as="style"><link rel="preload" href="/assets/js/app.269009d6.js" as="script"><link rel="preload" href="/assets/js/3.974b9720.js" as="script"><link rel="preload" href="/assets/js/8.cb9c08e5.js" as="script"><link rel="prefetch" href="/assets/js/10.5915e99b.js"><link rel="prefetch" href="/assets/js/11.ff732762.js"><link rel="prefetch" href="/assets/js/12.8bb86d91.js"><link rel="prefetch" href="/assets/js/13.18bde5b0.js"><link rel="prefetch" href="/assets/js/14.314fae34.js"><link rel="prefetch" href="/assets/js/15.0a4ff48f.js"><link rel="prefetch" href="/assets/js/16.ffaf1503.js"><link rel="prefetch" href="/assets/js/17.49a31a6e.js"><link rel="prefetch" href="/assets/js/18.7e6443bf.js"><link rel="prefetch" href="/assets/js/19.6880cc9a.js"><link rel="prefetch" href="/assets/js/20.1fb3f255.js"><link rel="prefetch" href="/assets/js/21.0cd39eea.js"><link rel="prefetch" href="/assets/js/22.392d85cb.js"><link rel="prefetch" href="/assets/js/23.f50adb15.js"><link rel="prefetch" href="/assets/js/24.43e324e1.js"><link rel="prefetch" href="/assets/js/25.e3663205.js"><link rel="prefetch" href="/assets/js/26.da141fb8.js"><link rel="prefetch" href="/assets/js/27.6c05235a.js"><link rel="prefetch" href="/assets/js/28.c124245a.js"><link rel="prefetch" href="/assets/js/29.d185870d.js"><link rel="prefetch" href="/assets/js/30.8587d422.js"><link rel="prefetch" href="/assets/js/31.dc382b56.js"><link rel="prefetch" href="/assets/js/32.20ba32e9.js"><link rel="prefetch" href="/assets/js/33.6ecd7de9.js"><link rel="prefetch" href="/assets/js/34.2b91c13a.js"><link rel="prefetch" href="/assets/js/35.66ed0733.js"><link rel="prefetch" href="/assets/js/36.ce7bca84.js"><link rel="prefetch" href="/assets/js/37.d00bb5ac.js"><link rel="prefetch" href="/assets/js/38.c3ecbb6f.js"><link rel="prefetch" href="/assets/js/39.308f4175.js"><link rel="prefetch" href="/assets/js/4.520f6ecc.js"><link rel="prefetch" href="/assets/js/40.f4b98bc2.js"><link rel="prefetch" href="/assets/js/41.95fae64f.js"><link rel="prefetch" href="/assets/js/42.fe4f877b.js"><link rel="prefetch" href="/assets/js/43.ca8f222e.js"><link rel="prefetch" href="/assets/js/44.4d4ee79e.js"><link rel="prefetch" href="/assets/js/45.79178b33.js"><link rel="prefetch" href="/assets/js/46.c3817d0b.js"><link rel="prefetch" href="/assets/js/47.b8252443.js"><link rel="prefetch" href="/assets/js/48.df9d5d3b.js"><link rel="prefetch" href="/assets/js/49.d812fdb2.js"><link rel="prefetch" href="/assets/js/5.f1a24a16.js"><link rel="prefetch" href="/assets/js/50.55cfcaa8.js"><link rel="prefetch" href="/assets/js/51.b8608b02.js"><link rel="prefetch" href="/assets/js/52.52c5e00d.js"><link rel="prefetch" href="/assets/js/53.3a9bd5fb.js"><link rel="prefetch" href="/assets/js/54.a4240c13.js"><link rel="prefetch" href="/assets/js/55.01c4b876.js"><link rel="prefetch" href="/assets/js/56.f29549a3.js"><link rel="prefetch" href="/assets/js/57.01438a04.js"><link rel="prefetch" href="/assets/js/58.29bb4626.js"><link rel="prefetch" href="/assets/js/59.e8b89408.js"><link rel="prefetch" href="/assets/js/6.8c478b39.js"><link rel="prefetch" href="/assets/js/60.bedb5381.js"><link rel="prefetch" href="/assets/js/61.bdfbe584.js"><link rel="prefetch" href="/assets/js/62.cb825d1a.js"><link rel="prefetch" href="/assets/js/63.a9516572.js"><link rel="prefetch" href="/assets/js/64.aa854ed7.js"><link rel="prefetch" href="/assets/js/65.285f883d.js"><link rel="prefetch" href="/assets/js/66.12849f37.js"><link rel="prefetch" href="/assets/js/67.c18b00e4.js"><link rel="prefetch" href="/assets/js/68.fa59daa9.js"><link rel="prefetch" href="/assets/js/69.e9d5dda4.js"><link rel="prefetch" href="/assets/js/7.cfb0d0ea.js"><link rel="prefetch" href="/assets/js/70.db126cce.js"><link rel="prefetch" href="/assets/js/71.6acb04cd.js"><link rel="prefetch" href="/assets/js/72.7ff0ab11.js"><link rel="prefetch" href="/assets/js/73.9a66da79.js"><link rel="prefetch" href="/assets/js/74.0a38a030.js"><link rel="prefetch" href="/assets/js/75.7b32dbb3.js"><link rel="prefetch" href="/assets/js/9.ad652cfe.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.110a1065.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8932179b.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/images/icons/logo.png" alt="lilei" class="logo"> <span class="site-name can-hide">lilei</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/Form/" class="nav-link router-link-active">
  Form
</a></div><div class="nav-item"><a href="/IDE/" class="nav-link">
  IDE
</a></div><div class="nav-item"><a href="/Java/" class="nav-link">
  Java
</a></div><div class="nav-item"><a href="/Other/" class="nav-link">
  Other
</a></div><div class="nav-item"><a href="/Python/" class="nav-link">
  Python
</a></div><div class="nav-item"><a href="/SQL/" class="nav-link">
  SQL
</a></div><div class="nav-item"><a href="/Shell/" class="nav-link">
  Shell
</a></div><div class="nav-item"><a href="/System/" class="nav-link">
  System
</a></div> <a href="https://github.com/woytu/notes-vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/Form/" class="nav-link router-link-active">
  Form
</a></div><div class="nav-item"><a href="/IDE/" class="nav-link">
  IDE
</a></div><div class="nav-item"><a href="/Java/" class="nav-link">
  Java
</a></div><div class="nav-item"><a href="/Other/" class="nav-link">
  Other
</a></div><div class="nav-item"><a href="/Python/" class="nav-link">
  Python
</a></div><div class="nav-item"><a href="/SQL/" class="nav-link">
  SQL
</a></div><div class="nav-item"><a href="/Shell/" class="nav-link">
  Shell
</a></div><div class="nav-item"><a href="/System/" class="nav-link">
  System
</a></div> <a href="https://github.com/woytu/notes-vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Form</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/Form/" aria-current="page" class="sidebar-link">介绍</a></li><li><a href="/Form/Go操作Windows.html" class="active sidebar-link">Go操作Windows</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Form/Go操作Windows.html#flag" class="sidebar-link">Flag</a></li><li class="sidebar-sub-header"><a href="/Form/Go操作Windows.html#设置壁纸" class="sidebar-link">设置壁纸</a></li><li class="sidebar-sub-header"><a href="/Form/Go操作Windows.html#操作注册表" class="sidebar-link">操作注册表</a></li></ul></li><li><a href="/Form/Go爬虫.html" class="sidebar-link">Go爬虫</a></li><li><a href="/Form/Go笔记.html" class="sidebar-link">Go笔记</a></li><li><a href="/Form/Go编译打包.html" class="sidebar-link">Go编译打包</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="go操作windows"><a href="#go操作windows" class="header-anchor">#</a> Go操作Windows</h1> <p></p><div class="table-of-contents"><ul><li><a href="#flag">Flag</a></li><li><a href="#设置壁纸">设置壁纸</a></li><li><a href="#操作注册表">操作注册表</a></li></ul></div><p></p> <h2 id="flag"><a href="#flag" class="header-anchor">#</a> Flag</h2> <blockquote><p>Golang调用windows下的dll动态库中的函数使用<code>syscall</code>调用</p></blockquote> <ul><li>GUI <a href="https://github.com/lxn/walk" target="_blank" rel="noopener noreferrer">https://github.com/lxn/walk<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <h2 id="设置壁纸"><a href="#设置壁纸" class="header-anchor">#</a> 设置壁纸</h2> <ul><li><a href="https://docs.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-systemparametersinfoa" target="_blank" rel="noopener noreferrer">https://docs.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-systemparametersinfoa<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li>main.go</li></ul> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
    <span class="token string">&quot;errors&quot;</span>
    <span class="token string">&quot;flag&quot;</span>
    <span class="token string">&quot;image/jpeg&quot;</span>
    <span class="token string">&quot;log&quot;</span>
    <span class="token string">&quot;os&quot;</span>
    <span class="token string">&quot;path/filepath&quot;</span>

    <span class="token string">&quot;github.com/golang/sys/windows/registry&quot;</span>
    <span class="token string">&quot;golang.org/x/image/bmp&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">type</span> WallpaperStyle <span class="token builtin">uint</span>

<span class="token keyword">func</span> <span class="token punctuation">(</span>wps WallpaperStyle<span class="token punctuation">)</span> <span class="token function">String</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">string</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> wallpaperStyles<span class="token punctuation">[</span>wps<span class="token punctuation">]</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> <span class="token punctuation">(</span>
    Fill    WallpaperStyle <span class="token operator">=</span> <span class="token boolean">iota</span> <span class="token comment">// 填充</span>
    Fit                           <span class="token comment">// 适应</span>
    Stretch                       <span class="token comment">// 拉伸</span>
    Tile                          <span class="token comment">// 平铺</span>
    Center                        <span class="token comment">// 居中</span>
    Cross                         <span class="token comment">// 跨区</span>

<span class="token punctuation">)</span>

<span class="token keyword">var</span> wallpaperStyles <span class="token operator">=</span> <span class="token keyword">map</span><span class="token punctuation">[</span>WallpaperStyle<span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span>
    <span class="token number">0</span><span class="token punctuation">:</span> <span class="token string">&quot;填充&quot;</span><span class="token punctuation">,</span>
    <span class="token number">1</span><span class="token punctuation">:</span> <span class="token string">&quot;适应&quot;</span><span class="token punctuation">,</span>
    <span class="token number">2</span><span class="token punctuation">:</span> <span class="token string">&quot;拉伸&quot;</span><span class="token punctuation">,</span>
    <span class="token number">3</span><span class="token punctuation">:</span> <span class="token string">&quot;平铺&quot;</span><span class="token punctuation">,</span>
    <span class="token number">4</span><span class="token punctuation">:</span> <span class="token string">&quot;居中&quot;</span><span class="token punctuation">,</span>
    <span class="token number">5</span><span class="token punctuation">:</span> <span class="token string">&quot;跨区&quot;</span><span class="token punctuation">}</span>

<span class="token keyword">var</span> <span class="token punctuation">(</span>
    bgFile       <span class="token builtin">string</span>
    bgStyle      <span class="token builtin">int</span>
    sFile        <span class="token builtin">string</span>
    waitTime     <span class="token builtin">int</span>
    activeScreen <span class="token builtin">bool</span>
    passwd       <span class="token builtin">bool</span>
<span class="token punctuation">)</span>

<span class="token keyword">var</span> <span class="token punctuation">(</span>
    regist registry<span class="token punctuation">.</span>Key
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    flag<span class="token punctuation">.</span><span class="token function">StringVar</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>bgFile<span class="token punctuation">,</span> <span class="token string">&quot;b&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;set bg file path.&quot;</span><span class="token punctuation">)</span>
    flag<span class="token punctuation">.</span><span class="token function">IntVar</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>bgStyle<span class="token punctuation">,</span> <span class="token string">&quot;style&quot;</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">&quot;set desktop WallpaperStyle&quot;</span><span class="token punctuation">)</span>

    flag<span class="token punctuation">.</span><span class="token function">BoolVar</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>activeScreen<span class="token punctuation">,</span> <span class="token string">&quot;a&quot;</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token string">&quot;set screen active.&quot;</span><span class="token punctuation">)</span>
    flag<span class="token punctuation">.</span><span class="token function">StringVar</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>sFile<span class="token punctuation">,</span> <span class="token string">&quot;s&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;set screen save file path.&quot;</span><span class="token punctuation">)</span>
    flag<span class="token punctuation">.</span><span class="token function">IntVar</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>waitTime<span class="token punctuation">,</span> <span class="token string">&quot;t&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">&quot;set screen save wait time.&quot;</span><span class="token punctuation">)</span>
    flag<span class="token punctuation">.</span><span class="token function">BoolVar</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>passwd<span class="token punctuation">,</span> <span class="token string">&quot;p&quot;</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token string">&quot;sets whether the screen saver requires the user to enter a password to display the Windows desktop. &quot;</span><span class="token punctuation">)</span>

<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    flag<span class="token punctuation">.</span><span class="token function">Parse</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">var</span> err <span class="token builtin">error</span>
    regist<span class="token punctuation">,</span> err <span class="token operator">=</span> registry<span class="token punctuation">.</span><span class="token function">OpenKey</span><span class="token punctuation">(</span>registry<span class="token punctuation">.</span>CURRENT_USER<span class="token punctuation">,</span> <span class="token string">`Control Panel\Desktop`</span><span class="token punctuation">,</span> registry<span class="token punctuation">.</span>ALL_ACCESS<span class="token punctuation">)</span>
    <span class="token function">checkErr</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
    <span class="token keyword">defer</span> regist<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token comment">// 设置桌面背景</span>
    <span class="token keyword">if</span> bgFile <span class="token operator">!=</span> <span class="token string">&quot;&quot;</span> <span class="token punctuation">{</span>
        style <span class="token operator">:=</span> <span class="token function">WallpaperStyle</span><span class="token punctuation">(</span>bgStyle<span class="token punctuation">)</span>
        <span class="token keyword">if</span> bgStyle <span class="token operator">&lt;</span> <span class="token number">0</span> <span class="token operator">||</span> bgStyle <span class="token operator">&gt;</span> <span class="token number">5</span> <span class="token punctuation">{</span>
            style <span class="token operator">=</span> Stretch
        <span class="token punctuation">}</span>
        <span class="token function">setDesktopWallpaper</span><span class="token punctuation">(</span>bgFile<span class="token punctuation">,</span> style<span class="token punctuation">)</span>
        log<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;设置桌面背景和位置 --&gt; %s, %s\n&quot;</span><span class="token punctuation">,</span> bgFile<span class="token punctuation">,</span> style<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    ok <span class="token operator">:=</span> <span class="token function">getScreenSaver</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    log<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;获取屏幕保护开关 --&gt; %t\n&quot;</span><span class="token punctuation">,</span> ok<span class="token punctuation">)</span>
    <span class="token comment">// 关闭屏幕保护</span>
    <span class="token keyword">if</span> ok <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>activeScreen <span class="token punctuation">{</span>
        regist<span class="token punctuation">.</span><span class="token function">DeleteValue</span><span class="token punctuation">(</span><span class="token string">&quot;SCRNSAVE.EXE&quot;</span><span class="token punctuation">)</span>
        log<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;关闭屏幕保护&quot;</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 设置屏幕保护</span>
    <span class="token keyword">if</span> sFile <span class="token operator">!=</span> <span class="token string">&quot;&quot;</span> <span class="token operator">&amp;&amp;</span> activeScreen <span class="token punctuation">{</span>
        err <span class="token operator">=</span> regist<span class="token punctuation">.</span><span class="token function">SetStringValue</span><span class="token punctuation">(</span><span class="token string">&quot;SCRNSAVE.EXE&quot;</span><span class="token punctuation">,</span> sFile<span class="token punctuation">)</span>
        <span class="token function">checkErr</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
        <span class="token function">setScreenSaver</span><span class="token punctuation">(</span>SPI_SETSCREENSAVEACTIVE<span class="token punctuation">,</span> TRUE<span class="token punctuation">)</span>
        log<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;设置屏幕保护 --&gt; %s\n&quot;</span><span class="token punctuation">,</span> sFile<span class="token punctuation">)</span>
        ok <span class="token operator">=</span> <span class="token function">getScreenSaver</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">if</span> ok <span class="token punctuation">{</span>
        <span class="token comment">// 设置屏幕保护时间</span>
        <span class="token keyword">if</span> waitTime <span class="token operator">&gt;</span> <span class="token number">0</span> <span class="token punctuation">{</span>
            <span class="token function">setScreenSaver</span><span class="token punctuation">(</span>SPI_SETSCREENSAVETIMEOUT<span class="token punctuation">,</span> <span class="token function">uint32</span><span class="token punctuation">(</span><span class="token number">60</span><span class="token operator">*</span>waitTime<span class="token punctuation">)</span><span class="token punctuation">)</span>
            log<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;设置屏幕保护等待时间 --&gt; %d分钟\n&quot;</span><span class="token punctuation">,</span> waitTime<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// 设置屏幕保护 在恢复时使用密码</span>
        <span class="token keyword">var</span> <span class="token punctuation">(</span>
            passwdSwitch <span class="token builtin">string</span>
            passwdBool   <span class="token builtin">uint32</span>
        <span class="token punctuation">)</span>
        <span class="token keyword">if</span> passwd <span class="token punctuation">{</span>
            passwdSwitch <span class="token operator">=</span> <span class="token string">&quot;1&quot;</span>
            passwdBool <span class="token operator">=</span> TRUE
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            passwdSwitch <span class="token operator">=</span> <span class="token string">&quot;0&quot;</span>
            passwdBool <span class="token operator">=</span> FALSE
        <span class="token punctuation">}</span>
        <span class="token comment">// XP / server 2003</span>
        <span class="token function">setRegistString</span><span class="token punctuation">(</span><span class="token string">&quot;ScreenSaverIsSecure&quot;</span><span class="token punctuation">,</span> passwdSwitch<span class="token punctuation">)</span>
        <span class="token comment">// vista or later</span>
        <span class="token keyword">if</span> <span class="token function">checkVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">setScreenSaver</span><span class="token punctuation">(</span>SPI_SETSCREENSAVESECURE<span class="token punctuation">,</span> passwdBool<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        log<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;设置屏幕保护恢复时是否使用密码 --&gt; %t\n&quot;</span><span class="token punctuation">,</span> passwd<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">checkErr</span><span class="token punctuation">(</span>err <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        log<span class="token punctuation">.</span><span class="token function">Fatal</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// http://blog.csdn.net/kfysck/article/details/8153264</span>
<span class="token comment">// Check that the OS is Vista or later (Vista is v6.0).</span>
<span class="token keyword">func</span> <span class="token function">checkVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">bool</span> <span class="token punctuation">{</span>
    version <span class="token operator">:=</span> <span class="token function">GetVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    major <span class="token operator">:=</span> version <span class="token operator">&amp;</span> <span class="token number">0xFF</span>
    <span class="token keyword">if</span> major <span class="token operator">&lt;</span> <span class="token number">6</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token boolean">false</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token boolean">true</span>
<span class="token punctuation">}</span>

<span class="token comment">// jpg转换为bmp</span>
<span class="token keyword">func</span> <span class="token function">ConvertedWallpaper</span><span class="token punctuation">(</span>bgfile <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span> <span class="token punctuation">{</span>
    file<span class="token punctuation">,</span> err <span class="token operator">:=</span> os<span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span>bgfile<span class="token punctuation">)</span>
    <span class="token function">checkErr</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
    <span class="token keyword">defer</span> file<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    img<span class="token punctuation">,</span> err <span class="token operator">:=</span> jpeg<span class="token punctuation">.</span><span class="token function">Decode</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span> <span class="token comment">//解码</span>
    <span class="token function">checkErr</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>

    bmpPath <span class="token operator">:=</span> os<span class="token punctuation">.</span><span class="token function">Getenv</span><span class="token punctuation">(</span><span class="token string">&quot;USERPROFILE&quot;</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">`\Local Settings\Application Data\Microsoft\Wallpaper1.bmp`</span>
    bmpfile<span class="token punctuation">,</span> err <span class="token operator">:=</span> os<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span>bmpPath<span class="token punctuation">)</span>
    <span class="token function">checkErr</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
    <span class="token keyword">defer</span> bmpfile<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    err <span class="token operator">=</span> bmp<span class="token punctuation">.</span><span class="token function">Encode</span><span class="token punctuation">(</span>bmpfile<span class="token punctuation">,</span> img<span class="token punctuation">)</span>
    <span class="token function">checkErr</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
    <span class="token keyword">return</span> bmpPath
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">setDesktopWallpaper</span><span class="token punctuation">(</span>bgFile <span class="token builtin">string</span><span class="token punctuation">,</span> style WallpaperStyle<span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
    ext <span class="token operator">:=</span> filepath<span class="token punctuation">.</span><span class="token function">Ext</span><span class="token punctuation">(</span>bgFile<span class="token punctuation">)</span>
    <span class="token comment">// vista 以下的系统需要转换jpg为bmp（xp、2003）</span>
    <span class="token keyword">if</span> <span class="token operator">!</span><span class="token function">checkVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> ext <span class="token operator">!=</span> <span class="token string">&quot;.bmp&quot;</span> <span class="token punctuation">{</span>
        <span class="token function">setRegistString</span><span class="token punctuation">(</span><span class="token string">&quot;ConvertedWallpaper&quot;</span><span class="token punctuation">,</span> bgFile<span class="token punctuation">)</span>
        bgFile <span class="token operator">=</span> <span class="token function">ConvertedWallpaper</span><span class="token punctuation">(</span>bgFile<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 设置桌面背景</span>
    <span class="token function">setRegistString</span><span class="token punctuation">(</span><span class="token string">&quot;Wallpaper&quot;</span><span class="token punctuation">,</span> bgFile<span class="token punctuation">)</span>

    <span class="token comment">/* 设置壁纸风格和展开方式
    在Control Panel\Desktop中的两个键值将被设置
    TileWallpaper
     0: 图片不被平铺
     1: 被平铺
    WallpaperStyle
     0:  0表示图片居中，1表示平铺
     2:  拉伸填充整个屏幕
     6:  拉伸适应屏幕并保持高度比
     10: 图片被调整大小裁剪适应屏幕保持纵横比
     22: 跨区
    */</span>
    <span class="token keyword">var</span> bgTileWallpaper<span class="token punctuation">,</span> bgWallpaperStyle <span class="token builtin">string</span>
    bgTileWallpaper <span class="token operator">=</span> <span class="token string">&quot;0&quot;</span>
    <span class="token keyword">switch</span> style <span class="token punctuation">{</span>
    <span class="token keyword">case</span> Fill<span class="token punctuation">:</span> <span class="token comment">// (Windows 7 or later)</span>
        bgWallpaperStyle <span class="token operator">=</span> <span class="token string">&quot;10&quot;</span>
    <span class="token keyword">case</span> Fit<span class="token punctuation">:</span> <span class="token comment">// (Windows 7 or later)</span>
        bgWallpaperStyle <span class="token operator">=</span> <span class="token string">&quot;6&quot;</span>
    <span class="token keyword">case</span> Stretch<span class="token punctuation">:</span>
        bgWallpaperStyle <span class="token operator">=</span> <span class="token string">&quot;2&quot;</span>
    <span class="token keyword">case</span> Tile<span class="token punctuation">:</span>
        bgTileWallpaper <span class="token operator">=</span> <span class="token string">&quot;1&quot;</span>
        bgWallpaperStyle <span class="token operator">=</span> <span class="token string">&quot;0&quot;</span>
    <span class="token keyword">case</span> Center<span class="token punctuation">:</span>
        bgWallpaperStyle <span class="token operator">=</span> <span class="token string">&quot;0&quot;</span>
    <span class="token keyword">case</span> Cross<span class="token punctuation">:</span> <span class="token comment">// win10 or later</span>
        bgWallpaperStyle <span class="token operator">=</span> <span class="token string">&quot;22&quot;</span>
    <span class="token punctuation">}</span>

    <span class="token function">setRegistString</span><span class="token punctuation">(</span><span class="token string">&quot;WallpaperStyle&quot;</span><span class="token punctuation">,</span> bgWallpaperStyle<span class="token punctuation">)</span>
    <span class="token function">setRegistString</span><span class="token punctuation">(</span><span class="token string">&quot;TileWallpaper&quot;</span><span class="token punctuation">,</span> bgTileWallpaper<span class="token punctuation">)</span>

    ok <span class="token operator">:=</span> <span class="token function">SystemParametersInfo</span><span class="token punctuation">(</span>SPI_SETDESKWALLPAPER<span class="token punctuation">,</span> FALSE<span class="token punctuation">,</span> <span class="token boolean">nil</span><span class="token punctuation">,</span> SPIF_UPDATEINIFILE<span class="token operator">|</span>SPIF_SENDWININICHANGE<span class="token punctuation">)</span>
    <span class="token keyword">if</span> <span class="token operator">!</span>ok <span class="token punctuation">{</span>
        <span class="token keyword">return</span> errors<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token string">&quot;Desktop background Settings fail.&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">setRegistString</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> value <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    err <span class="token operator">:=</span> regist<span class="token punctuation">.</span><span class="token function">SetStringValue</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> value<span class="token punctuation">)</span>
    <span class="token function">checkErr</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">setScreenSaver</span><span class="token punctuation">(</span>uiAction<span class="token punctuation">,</span> uiParam <span class="token builtin">uint32</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    ok <span class="token operator">:=</span> <span class="token function">SystemParametersInfo</span><span class="token punctuation">(</span>uiAction<span class="token punctuation">,</span> uiParam<span class="token punctuation">,</span> <span class="token boolean">nil</span><span class="token punctuation">,</span> SPIF_UPDATEINIFILE<span class="token operator">|</span>SPIF_SENDWININICHANGE<span class="token punctuation">)</span>
    <span class="token keyword">if</span> <span class="token operator">!</span>ok <span class="token punctuation">{</span>
        log<span class="token punctuation">.</span><span class="token function">Fatal</span><span class="token punctuation">(</span><span class="token string">&quot;Screen saver Settings fail.&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">getScreenSaver</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">bool</span> <span class="token punctuation">{</span>
    <span class="token boolean">_</span><span class="token punctuation">,</span> <span class="token boolean">_</span><span class="token punctuation">,</span> err <span class="token operator">:=</span> regist<span class="token punctuation">.</span><span class="token function">GetStringValue</span><span class="token punctuation">(</span><span class="token string">&quot;SCRNSAVE.EXE&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token boolean">false</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token boolean">true</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li>win.go</li></ul> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
    <span class="token string">&quot;syscall&quot;</span>
    <span class="token string">&quot;unsafe&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">const</span> <span class="token punctuation">(</span>
    <span class="token comment">// 获取屏幕保护开关</span>
    SPI_GETSCREENSAVEACTIVE <span class="token operator">=</span> <span class="token number">0x0010</span>
    <span class="token comment">// 设置屏保开关</span>
    SPI_SETSCREENSAVEACTIVE <span class="token operator">=</span> <span class="token number">0x0011</span>
    <span class="token comment">// 设置屏保等待时间</span>
    SPI_SETSCREENSAVETIMEOUT <span class="token operator">=</span> <span class="token number">0x000F</span>
    <span class="token comment">// 设备屏保在恢复时显示屏幕</span>
    SPI_SETSCREENSAVESECURE <span class="token operator">=</span> <span class="token number">0x0077</span>

    <span class="token comment">// 设置桌面背景</span>
    SPI_SETDESKWALLPAPER <span class="token operator">=</span> <span class="token number">0x0014</span>

    <span class="token comment">// 将新的系统范围的参数设置写入用户配置文件。</span>
    SPIF_UPDATEINIFILE <span class="token operator">=</span> <span class="token number">1</span>

    <span class="token comment">// 更新用户配置文件后广播WM_SETTINGCHANGE消息。</span>
    SPIF_SENDWININICHANGE <span class="token operator">=</span> <span class="token number">2</span>

    FALSE <span class="token operator">=</span> <span class="token number">0</span>
    TRUE  <span class="token operator">=</span> <span class="token number">1</span>
<span class="token punctuation">)</span>

<span class="token keyword">var</span> <span class="token punctuation">(</span>
    <span class="token comment">// Library</span>
    libuser32   <span class="token builtin">uintptr</span>
    libkernel32 <span class="token builtin">uintptr</span>

    <span class="token comment">// Functions</span>
    systemParametersInfo <span class="token builtin">uintptr</span>
    getVersion           <span class="token builtin">uintptr</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// Library</span>
    libuser32 <span class="token operator">=</span> <span class="token function">MustLoadLibrary</span><span class="token punctuation">(</span><span class="token string">&quot;user32.dll&quot;</span><span class="token punctuation">)</span>
    libkernel32 <span class="token operator">=</span> <span class="token function">MustLoadLibrary</span><span class="token punctuation">(</span><span class="token string">&quot;kernel32.dll&quot;</span><span class="token punctuation">)</span>
    <span class="token comment">// SystemParametersInfo有两个子函数针对不同的字符集：</span>
    <span class="token comment">// SystemParametersInfoW（Unicode）和SystemParametersInfoA（ANSI）</span>
    systemParametersInfo <span class="token operator">=</span> <span class="token function">MustGetProcAddress</span><span class="token punctuation">(</span>libuser32<span class="token punctuation">,</span> <span class="token string">&quot;SystemParametersInfoW&quot;</span><span class="token punctuation">)</span>
    getVersion <span class="token operator">=</span> <span class="token function">MustGetProcAddress</span><span class="token punctuation">(</span>libkernel32<span class="token punctuation">,</span> <span class="token string">&quot;GetVersion&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">MustLoadLibrary</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">uintptr</span> <span class="token punctuation">{</span>
    lib<span class="token punctuation">,</span> err <span class="token operator">:=</span> syscall<span class="token punctuation">.</span><span class="token function">LoadLibrary</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span>
    <span class="token comment">// user32 := syscall.NewLazyDLL(&quot;user32.dll&quot;)</span>
    <span class="token comment">// MessageBoxW := user32.NewProc(&quot;MessageBoxW&quot;)</span>
    <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token function">uintptr</span><span class="token punctuation">(</span>lib<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">MustGetProcAddress</span><span class="token punctuation">(</span>lib <span class="token builtin">uintptr</span><span class="token punctuation">,</span> name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">uintptr</span> <span class="token punctuation">{</span>
    addr<span class="token punctuation">,</span> err <span class="token operator">:=</span> syscall<span class="token punctuation">.</span><span class="token function">GetProcAddress</span><span class="token punctuation">(</span>syscall<span class="token punctuation">.</span><span class="token function">Handle</span><span class="token punctuation">(</span>lib<span class="token punctuation">)</span><span class="token punctuation">,</span> name<span class="token punctuation">)</span>
    <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token function">uintptr</span><span class="token punctuation">(</span>addr<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment">/*
之前已经通过注册表设置了壁纸的参数，但是还没有刷新配置。
调用Win32 API位于user32.dll中函数SystemParametersInfo设置桌面壁纸，它支持我们从系统中获得硬件和配置信息。
它有四个参数，第一个指明调用这个函数所要执行的操作，接下来的两个参数指明将要设置的数据，依据第一个参数的设定。
*/</span>
<span class="token comment">// uiAction：该参数指定要查询或设置的参数，换壁纸是SPI_SETDESKWALLPAPER（使得壁纸改变保存并持续可见）</span>
<span class="token comment">// uiParam：附加值</span>
<span class="token comment">// pvParam：要修改或者查询的缓冲区</span>
<span class="token comment">// fWinIni：指定用户配置文件是否被更新，如果是这样，则指定是否 WM_SETTINGCHANGE 将消息广播到所有的顶层窗口的更改通知他们，可以是0也可以是下列值：</span>
<span class="token comment">//      SPIF_UPDATEINIFILE：把新的系统参数的设置内容写入用户配置文件。</span>
<span class="token comment">//      SPIF_SENDCHANGE：在更新用户配置文件之后发送WM_SETTINGCHANGE消息。</span>
<span class="token comment">//      SPI_SENDWININICHANGE：与SPIF_SENDCHANGE一样。</span>
<span class="token comment">// 换壁纸我们要给uiAction指定SPI_SETDESKWALLPAPER标志位，然后把SPIF_UPDATEINIFILE传递给fWinIni。</span>
<span class="token keyword">func</span> <span class="token function">SystemParametersInfo</span><span class="token punctuation">(</span>uiAction<span class="token punctuation">,</span> uiParam <span class="token builtin">uint32</span><span class="token punctuation">,</span> pvParam unsafe<span class="token punctuation">.</span>Pointer<span class="token punctuation">,</span> fWinIni <span class="token builtin">uint32</span><span class="token punctuation">)</span> <span class="token builtin">bool</span> <span class="token punctuation">{</span>
    ret<span class="token punctuation">,</span> <span class="token boolean">_</span><span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> syscall<span class="token punctuation">.</span><span class="token function">Syscall6</span><span class="token punctuation">(</span>systemParametersInfo<span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token function">uintptr</span><span class="token punctuation">(</span>uiAction<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">uintptr</span><span class="token punctuation">(</span>uiParam<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">uintptr</span><span class="token punctuation">(</span>pvParam<span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token function">uintptr</span><span class="token punctuation">(</span>fWinIni<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> ret <span class="token operator">!=</span> <span class="token number">0</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">GetVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">int64</span> <span class="token punctuation">{</span>
    ret<span class="token punctuation">,</span> <span class="token boolean">_</span><span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> syscall<span class="token punctuation">.</span><span class="token function">Syscall</span><span class="token punctuation">(</span>getVersion<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token function">int64</span><span class="token punctuation">(</span>ret<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">stringToPointer</span><span class="token punctuation">(</span>str <span class="token builtin">string</span><span class="token punctuation">)</span> unsafe<span class="token punctuation">.</span>Pointer <span class="token punctuation">{</span>
    <span class="token keyword">return</span> unsafe<span class="token punctuation">.</span><span class="token function">Pointer</span><span class="token punctuation">(</span>syscall<span class="token punctuation">.</span><span class="token function">StringToUTF16Ptr</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="操作注册表"><a href="#操作注册表" class="header-anchor">#</a> 操作注册表</h2> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
    <span class="token string">&quot;golang.org/x/sys/windows/registry&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 创建：指定路径的项</span>
    <span class="token comment">// 路径：HKEY_CURRENT_USER\Software\Hello Go</span>
    key<span class="token punctuation">,</span> exists<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> registry<span class="token punctuation">.</span><span class="token function">CreateKey</span><span class="token punctuation">(</span>registry<span class="token punctuation">.</span>CURRENT_USER<span class="token punctuation">,</span> <span class="token string">`SOFTWARE\Hello Go`</span><span class="token punctuation">,</span> registry<span class="token punctuation">.</span>ALL_ACCESS<span class="token punctuation">)</span>
    <span class="token keyword">defer</span> key<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token comment">// 判断是否已经存在了</span>
    <span class="token keyword">if</span> exists <span class="token punctuation">{</span>
        <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">`键已存在`</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">`新建注册表键`</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 写入：32位整形值</span>
    key<span class="token punctuation">.</span><span class="token function">SetDWordValue</span><span class="token punctuation">(</span><span class="token string">`32位整形值`</span><span class="token punctuation">,</span> <span class="token function">uint32</span><span class="token punctuation">(</span><span class="token number">123456</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token comment">// 写入：64位整形值</span>
    key<span class="token punctuation">.</span><span class="token function">SetQWordValue</span><span class="token punctuation">(</span><span class="token string">`64位整形值`</span><span class="token punctuation">,</span> <span class="token function">uint64</span><span class="token punctuation">(</span><span class="token number">123456</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token comment">// 写入：字符串</span>
    key<span class="token punctuation">.</span><span class="token function">SetStringValue</span><span class="token punctuation">(</span><span class="token string">`字符串`</span><span class="token punctuation">,</span> <span class="token string">`hello`</span><span class="token punctuation">)</span>
    <span class="token comment">// 写入：字符串数组</span>
    key<span class="token punctuation">.</span><span class="token function">SetStringsValue</span><span class="token punctuation">(</span><span class="token string">`字符串数组`</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">`hello`</span><span class="token punctuation">,</span> <span class="token string">`world`</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token comment">// 写入：二进制</span>
    key<span class="token punctuation">.</span><span class="token function">SetBinaryValue</span><span class="token punctuation">(</span><span class="token string">`二进制`</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">{</span><span class="token number">0x11</span><span class="token punctuation">,</span> <span class="token number">0x22</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

    <span class="token comment">// 读取：字符串</span>
    s<span class="token punctuation">,</span> <span class="token boolean">_</span><span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> key<span class="token punctuation">.</span><span class="token function">GetStringValue</span><span class="token punctuation">(</span><span class="token string">`字符串`</span><span class="token punctuation">)</span>
    <span class="token function">println</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span>

    <span class="token comment">// 读取：一个项下的所有子项</span>
    keys<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> key<span class="token punctuation">.</span><span class="token function">ReadSubKeyNames</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> key_subkey <span class="token operator">:=</span> <span class="token keyword">range</span> keys <span class="token punctuation">{</span>
        <span class="token comment">// 输出所有子项的名字</span>
        <span class="token function">println</span><span class="token punctuation">(</span>key_subkey<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 创建：子项</span>
    subkey<span class="token punctuation">,</span> <span class="token boolean">_</span><span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> registry<span class="token punctuation">.</span><span class="token function">CreateKey</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> <span class="token string">`子项`</span><span class="token punctuation">,</span> registry<span class="token punctuation">.</span>ALL_ACCESS<span class="token punctuation">)</span>
    <span class="token keyword">defer</span> subkey<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token comment">// 删除：子项</span>
    <span class="token comment">// 该键有子项，所以会删除失败</span>
    <span class="token comment">// 没有子项，删除成功</span>
    registry<span class="token punctuation">.</span><span class="token function">DeleteKey</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> <span class="token string">`子项`</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/woytu/notes-vuepress/edit/master//Form/Go操作Windows.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/Form/" class="prev router-link-active">
        介绍
      </a></span> <span class="next"><a href="/Form/Go爬虫.html">
        Go爬虫
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.269009d6.js" defer></script><script src="/assets/js/3.974b9720.js" defer></script><script src="/assets/js/8.cb9c08e5.js" defer></script>
  </body>
</html>
